package gov.pbc.nn.kjgl.gzjhgl.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import gov.pbc.nn.core.bdf.service.UserService.DefaultUserRowMapper;
import gov.pbc.nn.core.dao.AbstractHibernateDao;
import gov.pbc.nn.core.dao.PbcnnCoreJdbcDao;
import gov.pbc.nn.core.utils.SecurityUtils;
import gov.pbc.nn.kjgl.Constants;
import gov.pbc.nn.kjgl.entity.WorkPlan;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

/**
 * 描述：本类主要为了操作工作计划实体类的数据操作
 * 
 * @author jack.zhao
 * 
 */
@Repository
public class WorkPlanDao extends AbstractHibernateDao {
	/**
	 * workPlan的分页加载数据操作
	 * 
	 * @auther jack.zhao
	 * @param page
	 * @param criteria
	 * 
	 */
	public void getPage(Page<WorkPlan> page, Criteria criteria) {
		DetachedCriteria query = this.buildDetachedCriteria(criteria, WorkPlan.class);
		query.add(Restrictions.or(Restrictions.eq("invalid", false), Restrictions.isNull("invalid")));
		query.add(Restrictions.eq("createUser", ContextHolder.getLoginUser().getUsername()));
		// query.addOrder(Order.desc("state"));
		query.addOrder(Order.desc("createDate"));
		// query.add(Restrictions.eq("state", "0"));
		this.pagingQuery(page, query);
	}

	/**
	 * 科长审批需要数据加载
	 * 
	 * @auther jack.zhao
	 * @param page
	 * @param criteria
	 * 
	 */
	public void getKezhangPage(Page<WorkPlan> page, Criteria criteria) {
		DetachedCriteria query = this.buildDetachedCriteria(criteria, WorkPlan.class);
		query.add(Restrictions.or(Restrictions.eq("invalid", false), Restrictions.isNull("invalid")));
		query.add(Restrictions.or(Restrictions.eq("state", "10"), Restrictions.eq("state", "50")));
		query.add(Restrictions.eq("kezhangId", ContextHolder.getLoginUser().getUsername()));
		// query.addOrder(Order.desc("state"));
		query.addOrder(Order.desc("updateDate"));
		this.pagingQuery(page, query);
	}

	/**
	 * 处长dateset的数据分页加载！
	 * 
	 * @auther jack.zhao
	 * @param page
	 * @param criteria
	 * 
	 */
	public void getChuzhangPage(Page<WorkPlan> page, Criteria criteria) {
		DetachedCriteria query = this.buildDetachedCriteria(criteria, WorkPlan.class);
		query.add(Restrictions.or(Restrictions.eq("invalid", false), Restrictions.isNull("invalid")));
		query.add(Restrictions.eq("state", "20"));
		query.add(Restrictions.eq("chuzhangId", ContextHolder.getLoginUser().getUsername()));
		// query.addOrder(Order.desc("state"));
		query.addOrder(Order.desc("updateDate"));
		this.pagingQuery(page, query);
	}

	/**
	 * 我的计划数据分页加载。 得到计划进行中状态为30的计划，并且计划负责人是当前登录人
	 * 
	 * @auther jack.zhao
	 * @param page
	 * @param criteria
	 * 
	 */
	public void myWorkPlanPage(Page<WorkPlan> page, Criteria criteria) {
		DetachedCriteria query = this.buildDetachedCriteria(criteria, WorkPlan.class);
		query.add(Restrictions.or(Restrictions.eq("invalid", false), Restrictions.isNull("invalid")));
		query.add(Restrictions.eq("fuzerenId", ContextHolder.getLoginUser().getUsername()));
		query.add(Restrictions.eq("state", "30"));
		query.addOrder(Order.desc("createDate"));
		this.pagingQuery(page, query);
	}

	/**
	 * 查询所有状态不等于60的状态。状态等于60是已经完成的计划 ！查询所有未完成的计划
	 * 
	 * @auther jack.zhao
	 * @param page
	 * @param criteria
	 * 
	 */
	public void searchAll(Page<WorkPlan> page, Criteria criteria) {
		DetachedCriteria query = this.buildDetachedCriteria(criteria, WorkPlan.class);
		query.add(Restrictions.or(Restrictions.eq("invalid", false), Restrictions.isNull("invalid")));
		query.add(Restrictions.ne("state", "60"));
		// query.addOrder(Order.desc("state"));
		query.addOrder(Order.desc("createDate"));
		this.pagingQuery(page, query);
	}

	/**
	 * 
	 * 查询所有状态等于60的状态，状态等于60是已经完成的计划 ！ 查询所有计划完成的计划
	 * 
	 * @auther jack.zhao
	 * @param page
	 * @param criteria
	 * 
	 */
	public void searchAllOver(Page<WorkPlan> page, Criteria criteria) {
		DetachedCriteria query = this.buildDetachedCriteria(criteria, WorkPlan.class);
		query.add(Restrictions.or(Restrictions.eq("invalid", false), Restrictions.isNull("invalid")));
		query.add(Restrictions.eq("state", "60"));
		// query.addOrder(Order.desc("state"));
		query.addOrder(Order.desc("createDate"));
		this.pagingQuery(page, query);
	}

	/**
	 * 查找所有后台实体不等于0的工作计划！
	 * 
	 * @auther jack.zhao
	 * @param page
	 * @param criteria
	 * 
	 */
	public void WorkPlanByState(Page<WorkPlan> page, Criteria criteria) {
		DetachedCriteria query = this.buildDetachedCriteria(criteria, WorkPlan.class);
		query.add(Restrictions.or(Restrictions.eq("invalid", false), Restrictions.isNull("invalid")));
		query.add(Restrictions.ne("state", "0"));
		this.pagingQuery(page, query);
	}

	// * * 　　 へ　　　　　／|
	// 　　/＼7　　　 ∠＿/
	// 　 /　│　　 ／　／ 你瞅啥，没见过那么美的分割线。。。。。
	// 　│　Z ＿,＜　／　　 /`ヽ
	// 　│　　　　　ヽ　　 /　　〉
	// 　 Y　　　　　`　 /　　/
	// 　ｲ●　､　●　　⊂⊃〈　　/
	// 　()　 へ　　　　|　＼〈
	// 　　>ｰ ､_　 ィ　 │ ／／
	// 　 / へ　　 /　ﾉ＜| ＼＼
	// 　 ヽ_ﾉ　　(_／　 │／／
	// 　　7　　　　　　　|／
	// 　　＞―r￣￣`ｰ―＿

	/**
	 * 后台根据状态操作数据库
	 * 
	 * @auther jack.zhao
	 * @param list
	 * 
	 */
	public void updateWorkPlan(WorkPlan list) {
		EntityState state = EntityUtils.getState(list);
		if (state.equals(EntityState.NEW)) {
			this.save(list);
		} else if (state.equals(EntityState.DELETED)) {
			this.delete(list);
		} else if (state.equals(EntityState.MODIFIED)) {
			// list.setState(workState);
			this.update(list);
		}
	}

	/**
	 * 根据id进行删除
	 * 
	 * @auther jack.zhao
	 * @param id
	 * 
	 */
	public void deleteWorkPlanById(String id) {
		this.delete(id, WorkPlan.class);
	}

	/**
	 * 根据id进行提交数据,并且改变数据的状态
	 * 
	 * @auther jack.zhao
	 * @param id
	 * 
	 */
	public void tjWorkPlan(String id) {
		// this.delete(id, WorkPlan.class);
		WorkPlan workplan = getWorkPlanById(id);
		if (workplan != null) {
			workplan.setState("10");
			this.update(workplan);
		}

	}

	/**
	 * 根据Id得到相应的实体
	 * 
	 * @auther jack.zhao
	 * @param id
	 * @return
	 * 
	 */
	public WorkPlan getWorkPlanById(String id) {
		String hql = "from WorkPlan where invalid=false and id=:id";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		List<WorkPlan> ls = this.query(hql, map);
		if (ls.size() > 0) {
			return ls.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 
	 * 传入多个id，并且传入负责人name和id，把数据进行保存
	 * 
	 * @auther jack.zhao
	 * @param parameter
	 * 
	 */
	public void tjfzr(Map<String, Object> parameter) {
		@SuppressWarnings("unchecked")
		List<String> ids = (List<String>) parameter.get("ids");
		String fuzerenName = (String) parameter.get("fuzerenName");
		String fuzerenId = (String) parameter.get("fuzerenId");
		if (ids.size() > 0) {
			for (String id : ids) {
				WorkPlan workPlan = this.getWorkPlanById(id);
				// workPlan.setState("30");
				workPlan.setFuzeren(fuzerenName);
				workPlan.setFuzerenId(fuzerenId);
				this.update(workPlan);
			}
		}
	}

	/**
	 * 提交到处长 处长审核
	 * 
	 * @auther jack.zhao
	 * @param id
	 * 
	 */
	public void tjChuzhangWorkPlan(String id) {
		WorkPlan workPlan = this.getWorkPlanById(id);
		String deptId = ContextHolder.getLoginUser().getDepts().get(0).getId();
		List<IUser> users = SecurityUtils.getUserByRoleForDept(deptId, Constants.ROLE_CZ);
		String chuzhang = null;
		if (users.size() >= 0) {
			chuzhang = users.get(0).getUsername();
		}
		if (workPlan != null) {
			workPlan.setState("20");
			workPlan.setChuzhangId(chuzhang);
			this.update(workPlan);

		}
	}

	/**
	 * 
	 * 
	 * @auther jack.zhao
	 * @param deptId
	 * @return
	 * 
	 */
	public List<IUser> getUserByDept(String deptId) {
		String sql = "SELECT U.* FROM T_USER U INNER JOIN bdf2_user_dept ud ON U.USERNAME_ = ud.USERNAME_ WHERE ud.DEPT_ID_ = ?";
		PbcnnCoreJdbcDao jdbc = ContextHolder.getBean(PbcnnCoreJdbcDao.BEAN_ID);
		List<IUser> users = jdbc.getJdbcTemplate().query(sql, new Object[] { deptId }, new DefaultUserRowMapper());
		return users;
	}

	/**
	 * 科长驳回的数据库操作
	 * 
	 * @auther jack.zhao
	 * @param id
	 * 
	 */
	public void kezhangBack(String id) {
		WorkPlan workPlan = this.getWorkPlanById(id);
		if (workPlan != null) {
			workPlan.setState("40");
			this.update(workPlan);
		}
	}

	// * * 　　 へ　　　　　／|
	// 　　/＼7　　　 ∠＿/
	// 　 /　│　　 ／　／ 你瞅啥，没见过那么美的分割线。。。。。
	// 　│　Z ＿,＜　／　　 /`ヽ
	// 　│　　　　　ヽ　　 /　　〉
	// 　 Y　　　　　`　 /　　/
	// 　ｲ●　､　●　　⊂⊃〈　　/
	// 　()　 へ　　　　|　＼〈
	// 　　>ｰ ､_　 ィ　 │ ／／
	// 　 / へ　　 /　ﾉ＜| ＼＼
	// 　 ヽ_ﾉ　　(_／　 │／／
	// 　　7　　　　　　　|／
	// 　　＞―r￣￣`ｰ―＿

	/**
	 * 
	 * 处长不同意，更改状态
	 * 
	 * @auther jack.zhao
	 * @param id
	 * 
	 */
	public void chuzhangBack(String id) {
		WorkPlan workPlan = this.getWorkPlanById(id);
		if (workPlan != null) {
			workPlan.setState("50");
			this.update(workPlan);
		}
	}

	/**
	 * 如果处长同意，把workPaln更改为执行状态ing
	 * 
	 * @auther jack.zhao
	 * @param id
	 * 
	 */
	public void executePlan(String id) {
		WorkPlan workPlan = this.getWorkPlanById(id);
		if (workPlan != null) {
			workPlan.setState("30");
			workPlan.setWorkState("进行中");
			this.update(workPlan);
		}
	}
}
